class Message < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
  belongs_to :category
  has_many   :files, :class_name=>'Files'
  has_many   :comments,:dependent=>:destroy
  after_create :log_save
  after_destroy :log_destroy

def log_save
  Log.info(self.project,
       "<a href=/projects/#{self.project_id}/messages/#{self.id}>#{self.name}</a>",
       1,
       "Posted by",
       self.user
    )
end

def log_destroy
  log = Log.find(:first,:conditions=>["project_id = ? and name = ? and type_id=? and label=? and user_id = ?",self.project.id,
       "<a href=/projects/#{self.project_id}/messages/#{self.id}>#{self.name}</a>",
       1,
       "Posted by",
       self.user.id])
       log.destroy unless log.nil?
end
end
